﻿using PC_Touch.DataOP;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.Threading;
using HslCommunication;

namespace PC_Touch
{
    public partial class SettingsQG : Form
    {
        public SettingsQG()
        {
            InitializeComponent();
            x = this.Width;
            y = this.Height;
            setTag(this);
            this.Resize += new EventHandler(this.Form_Resize);
            setZH_EN();
        }
        private void setZH_EN()
        {
            if (LanguageHelper.zh_enFlag == "中文")
            {
            }
            else
            {
                uiTitlePanel1.Text = "Left test bench cylinder control";
                uiTitlePanel2.Text = "Right test bench cylinder control";
                uiMarkLabel1.Text = "LeftClampingCylinder1";
                uiMarkLabel2.Text = "LeftClampingCylinder2";
                uiMarkLabel4.Text = "LeftSidePunchingCylinder";
                uiMarkLabel3.Text = "LeftSideRangingCompressionCylinder";
                uiMarkLabel6.Text = "LeftRangingJackingCylinder";
                uiMarkLabel5.Text = "LeftDistanceSensor";

                uiMarkLabel12.Text = "RightClampingCylinder1";
                uiMarkLabel8.Text = "RightClampingCylinder2";
                uiMarkLabel7.Text = "RightSidePunchingCylinder";
                uiMarkLabel11.Text = "RightSideRangingCompressionCylinder";
                uiMarkLabel10.Text = "RightRangingJackingCylinder";
                uiMarkLabel9.Text = "RightDistanceSensor";
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ReadPLCStatuesOnce();

            //监听事件初始化
            //PLCHelper.TriggerEventInit();
            //PLC轮询读取触发信号

        }
        public static bool[] ByteToBoolArray(byte b)
        {
            bool[] bits = new bool[8];

            for (int i = 0; i < 8; i++)
            {
                // 检查当前位是否为1，并将结果存储到对应的数组位置
                bits[i] = (b & (1 << (7 - i))) != 0;
            }

            return bits;
        }
        private void ReadPLCStatuesOnce()
        {
            if (PLCHelper.siemensConnect.IsSuccess)
            {
                // var by = PLCHelper.siemensTcpNet.ReadBool("Q104.4",12).Content;//读取气阀状态
                var q100_108 = PLCHelper.siemensTcpNet.Read("Q100", 9).Content;
                var q100 = ByteToBoolArray(q100_108[0]);
                var q101 = ByteToBoolArray(q100_108[1]);
                var q102 = ByteToBoolArray(q100_108[2]);
                var q103 = ByteToBoolArray(q100_108[3]);
                var q108 = ByteToBoolArray(q100_108[8]);



                左侧压紧气缸1.Active = q100[0];// PLCHelper.siemensTcpNet.ReadBool("Q100.0").Content; 
                左侧压紧气缸2.Active = q100[2];// PLCHelper.siemensTcpNet.ReadBool("Q100.2").Content;
                左侧打点气缸.Active = q101[0];//PLCHelper.siemensTcpNet.ReadBool("Q101.0").Content;
                左侧测距压紧气缸.Active = q100[4];//PLCHelper.siemensTcpNet.ReadBool("Q100.4").Content;
                左侧测距顶升气缸.Active = q108[1];//PLCHelper.siemensTcpNet.ReadBool("Q108.1").Content;
                左侧测距传感器.Active = q101[6];//PLCHelper.siemensTcpNet.ReadBool("Q101.6").Content;

                右侧压紧气缸1.Active = q102[0];// PLCHelper.siemensTcpNet.ReadBool("Q102.0").Content;
                右侧压紧气缸2.Active = q102[2];// PLCHelper.siemensTcpNet.ReadBool("Q102.2").Content;
                右侧打点气缸.Active = q103[0];// PLCHelper.siemensTcpNet.ReadBool("Q103.0").Content;
                右侧测距压紧气缸.Active = q102[4];//PLCHelper.siemensTcpNet.ReadBool("Q102.4").Content;
                右侧测距顶升气缸.Active = q108[3];// PLCHelper.siemensTcpNet.ReadBool("Q108.3").Content;
                右侧测距传感器.Active = q101[7];// PLCHelper.siemensTcpNet.ReadBool("Q101.7").Content;

            }
            else 
            {
                MessageBox.Show("PLC not connect!");
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            TaskManagement taskManagement = new TaskManagement();
            taskManagement.Show();
        }

        #region 控件缩放
        private float x = 1;
        private float y = 1;
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        private void Form_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / x;
            float newy = (this.Height) / y;
            setControls(newx, newy, this);
        }
        #endregion
        private void uiSwitch12_ValueChanged(object sender, bool value)
        {
            if (左侧测距顶升气缸.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.20.1", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.20.1", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.20.2", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.20.2", false);
            }
        }
        private void uiSwitch1_ValueChanged(object sender, bool value)
        {
            if (左侧压紧气缸1.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.0.0", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.0.0", false);
            }
            else 
            {
                PLCHelper.siemensTcpNet.Write("DB10.0.1", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.0.1", false);
            }
        }

        private void uiSwitch4_ValueChanged(object sender, bool value)
        {
            if (左侧压紧气缸2.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.0.2", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.0.2", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.0.3", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.0.3", false);
            }
        }

        private void qf3_ValueChanged(object sender, bool value)
        {
            if (左侧打点气缸.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.1.0", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.1.0", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.1.1", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.1.1", false);
            }
        }

        private void qf4_ValueChanged(object sender, bool value)
        {
            if (左侧测距压紧气缸.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.0.4", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.0.4", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.0.5", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.0.5", false);
            }
        }

        private void qf6_ValueChanged(object sender, bool value)
        {
            if (左侧测距传感器.Active)//气阀6控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.0.6", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.0.6", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.0.7", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.0.7", false);
            }
        }

        private void timerRead_Tick(object sender, EventArgs e)
        {
            if (PLCHelper.siemensConnect.IsSuccess)
            {
                string[] addresses = new string[] { "DB101.0", "DB101.4" };
                OperateResult<byte[]> readResult = PLCHelper.siemensTcpNet.Read(addresses, new ushort[] {4,4});
                if (readResult.IsSuccess)
                {
                    // 解析读取到的数据
                    // float value2 = BitConverter.ToSingle(readResult.Content, 4);
                    Zcj.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 0).ToString("F3");
                    Ycj.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 4).ToString("F3");
                }
            }
        }
    }
}
